package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.service.IAlertDataService;
import com.zzyl.nursing.vo.AlertDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 报警数据Service业务层处理
 *
 * @author Lorine
 * @date 2025-03-14
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return alertDataMapper.selectById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertDataDto 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertDataVo> selectAlertDataList(AlertDataDto alertDataDto) {
        return alertDataMapper.selectAlertDataList(alertDataDto);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return alertDataMapper.insert(alertData);
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return alertDataMapper.updateById(alertData);
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return alertDataMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return alertDataMapper.deleteById(id);
    }

    /**
     * 处理报警数据
     *
     * @param alertData
     */
    @Override
    public void handleAlertData(AlertData alertData) {
        //查询报警数据是否存在
        AlertData find = alertDataMapper.selectById(alertData.getId());
        if (ObjUtil.isNull(find)) {
            throw new BaseException("报警数据不存在");
        }
        if (find.getStatus().equals(1)) {
            throw new BaseException("报警已处理");
        }
        find.setStatus(1);
        find.setProcessingResult(alertData.getProcessingResult());
        find.setProcessingTime(alertData.getProcessingTime());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        find.setProcessorName(loginUser.getUser().getUserName());
        find.setProcessorId(loginUser.getUser().getUserId());
        updateById(find);
    }
}
